package com.securitydemo.config;

import com.sun.deploy.net.HttpResponse;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.impl.FileUploadIOException;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalException {

    @ExceptionHandler(value = RuntimeException.class)
    public String runtimeProcess(HttpRequest request, HttpResponse response,RuntimeException runtimeException) {
        return "yong xing shi";
    }

    @ExceptionHandler(value = FileUploadException.class)
    public String runtimeProcess(HttpRequest request, HttpResponse response, FileUploadException ex) {
        String msg;
        if (ex.getCause().getCause() instanceof FileUploadException) {
            msg = "上传文件过大[单文件大小不得超过10M]";
        } else if (ex.getCause().getCause() instanceof FileUploadIOException) {
            msg = "上传文件过大[总上传文件大小不得超过10M]";
        } else {
            msg = "上传文件失败";
        }
        return msg;
    }
}
